package com.xiaoshuidi.cloud.module.contract.controller.app.contract;


import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.OrderDetaillAndOrderFeeVo;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.rentOrder.RentOrderInfoVO;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.AppRentOrderCreateReq;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.enums.bill.OrderTypeEnum;
import com.xiaoshuidi.cloud.module.contract.service.bill.RentOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.error;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 账单查询")
@RestController("AppRentOrderController2")
@RequestMapping(ApiConstants.NAME + "/app/order")
@Validated
public class AppRentOrderController {
    @Resource
    private RentOrderService rentOrderService;

    /**
    * 查询待支付账单 已支付账单
    * */
    @GetMapping("/get")
    @Operation(summary = "查询租客账单")
    public CommonResult createRentOrder(@RequestParam("payStatus")String payStatus) {
        try {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            List<OrderDetaillAndOrderFeeVo> order = rentOrderService.getOrder(loginUserId, payStatus);
            return success(order);
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    /**
    * 查询 待开票 账单 和已开票
    * */
    @GetMapping("/get-invoice-order")
    @Operation(summary = "查询开票账单和待开票账单")
    public CommonResult getInvoiceOrder(@RequestParam("invoiceStatus")Integer invoiceStatus) {
        try {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            List<OrderDetaillAndOrderFeeVo> order = rentOrderService.getInvoiceOrder(loginUserId, invoiceStatus);
            return success(order);
        }catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    @GetMapping("/get-order")
    @Operation(summary = "租客账单待处理、未出账、已处理")
    @Parameter(name = "roomId", description = "房间id", required = true, example = "1024")
    public CommonResult<RentOrderInfoVO> getBill(@RequestParam("id") Long id) {
        try {
            RentOrderInfoVO bill = rentOrderService.getRentOrderByRoomId(id, Arrays.asList(OrderTypeEnum.DEFAULT.getCode(),OrderTypeEnum.CUSTOM.getCode(),
                    OrderTypeEnum.EXTENSION.getCode(),OrderTypeEnum.METER.getCode()));
            return success(bill);
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }


    @PostMapping("/create")
    @Operation(summary = "创建杂费账单")
    public CommonResult createRentOrder(@Valid @RequestBody AppRentOrderCreateReq req) {
        req.setOrderType(OrderTypeEnum.CUSTOM.getCode());
        try {
            rentOrderService.createAppRentOrder(req);
            return success();
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

}
